##### sample precision-recall ROC Curves

###############################################################
###############################################################
#########
######### different_goldstandards.R
######### author: Samir Rachid Zaim
######### date: 10/18/2018
#########
######### input: cell line MCF7 data
######### output:
###############################################################
###############################################################

FDR.cutoff=.05

require(data.table)
require(precrec)
require(ggplot2)
source('~/Dropbox/Lab-Tools-Files/R-Lab-Tools/RNA-Seq Tools/sourceDir.R')
sourceDir('~/Dropbox/Lab-Tools-Files/R-Lab-Tools/RNA-Seq Tools/iDEG')
sourceDir('~/Dropbox/Lab-Tools-Files/R-Lab-Tools/RNA-Seq Tools')

p1 = fread("~/Dropbox/iDEG-2018/clinical validation/data/sample-562-574.csv")
p2 = fread("~/Dropbox/iDEG-2018/clinical validation/data/sample-563-577.csv")
p3 = fread("~/Dropbox/iDEG-2018/clinical validation/data/sample-564-572.csv")
p4 = fread("~/Dropbox/iDEG-2018/clinical validation/data/sample-565-576.csv")
p5 = fread("~/Dropbox/iDEG-2018/clinical validation/data/sample-566-570.csv")
p6 = fread("~/Dropbox/iDEG-2018/clinical validation/data/sample-568-575.csv")
p7 = fread("~/Dropbox/iDEG-2018/clinical validation/data/sample-569-571.csv")

#### generate cohort matrix
count.mat = cbind(p1,p2[,2:3,with=F],p3[,2:3,with=F],p4[,2:3,with=F],
                  p5[,2:3,with=F],p6[,2:3,with=F],p7[,2:3,with=F])
pairs <- c('562-574','563-577','564-572','565-576','566-570','568-575','569-571')

#### randomly sample 4 isogenic pairs to use as the gold-standard
#### and use the remaining 3 for training
set.seed(1111)
gs.pairs <- sample(pairs, 4)
tst.pairs<-  pairs[ !  pairs %in% gs.pairs]

## find samples using string matching
idx.gs <- grep( paste(unlist( strsplit(gs.pairs, '-')),collapse='|') , colnames(count.mat))
gs.mat <- as.data.frame(count.mat[, idx.gs, with=F]); row.names(gs.mat) <- p1$V1
ts.mat <- as.data.frame(count.mat[, -c(1,idx.gs), with=F]); row.names(ts.mat) <- p1$V1

#### determine goldstandard technique
techniques = c('NOISeq','edgeR','DESeq','DESeq2','DEGseq','iDEG')
gs.technique = 'edgeR'

evaluators <- techniques[!techniques %in% gs.technique]


###############################################################
###############################################################
####
#### edgeR goldstandard


#### using individual samples
#### to form n-of-1 predictions

## edgeR as Gold standard
DEG_gs.edgeR <- DEG_edgeR(countTable = gs.mat,
                          conditions = rep(c('normal','tumor'),4),
                          replicateType = 'genetically identical model organisms',
                          disper = .1)

y_true <- p.adjust(DEG_gs.edgeR[[1]]$PValue, method='BY') < .005

i =1   
  
## NOISeq
NOISeq.preds <- DEG_noiseq(ts.mat[, c(i,i+1)], conditions =c('normal','tumor'))
NOISeq.preds <- NOISeq.preds$prob
NOISeq.preds[which(is.na(NOISeq.preds))] <- 0 

## DESeq
conditions = c('normal','tumor')
countTable = ts.mat[,c(i,i+1)]; colnames(countTable) <-  conditions
DESeq.preds  <- DEG_DESeq(countTable, conditions = conditions )
DESeq.preds  <-DESeq.preds$padj
DESeq.preds[which(is.na(DESeq.preds))] <- 1


## DESeq2
DESeq2.preds <- DEG_DESeq2(countTable, conditions= conditions)
DESeq2.preds <- DESeq2.preds$padj
DESeq2.preds[which(is.na(DESeq2.preds))] <- 1

## DEGseq
DEGseq.preds <- DEG_DEGseq(ts.mat[, c(i,i+1)], FDR_threshold= .1)
DEGseq.preds <- DEGseq.preds[order(DEGseq.preds$GeneNames),]
DEGseq.preds <- DEGseq.preds[,'q.value.Benjamini.et.al..1995.']
DEGseq.preds[which(is.na(DEGseq.preds))] <- 1 

## iDEG
baseline = ts.mat[,i]; names(baseline) <- row.names(ts.mat)
case     = ts.mat[,i+1]; names(case)     <- row.names(ts.mat)

iDEG.preds  <-iDEG(baseline, case, normalization=F, dataDistribution='NB')
iDEG.preds <- iDEG.preds[[1]]$local_fdr

## mixEnrich
mixEnrich.preds <- compute_nof1_MixEnrich_transcript(baseline, case, row.names(ts.mat) )
mixEnrich.preds  <- mixEnrich.preds$posterior_prob_RP

sscurves = evalmod(scores = list(1-iDEG.preds, 1-DEGseq.preds, NOISeq.preds,
                                 1-DESeq.preds, 1-DESeq2.preds, mixEnrich.preds, rowMeans(cbind(1-DEGseq.preds, NOISeq.preds,
                                                                                                mixEnrich.preds))),
                   labels= y_true,
                   modnames=c('iDEG','DEGseq','NOISeq','DESeq','DESeq2', 'Mixture Model','Ensemble'))


pdf('~/Dropbox/TBC-2018-single-subject-methods-accuracies/Figures/sample_precision_recall.pdf')
autoplot(sscurves, 'PRC')+  theme_bw() +
  theme(plot.title = element_text(size=rel(2)),
        axis.text = element_text(size=rel(1.3)),
        axis.title = element_text(size=rel(1.7)),
        legend.text=element_text(size=rel(2)))
autoplot(sscurves, 'ROC') +  theme_bw() +
  theme(plot.title = element_text(size=rel(2)),
        axis.text = element_text(size=rel(1.3)),
    axis.title = element_text(size=rel(1.7)),
    legend.text=element_text(size=rel(2)))

dev.off()

